package com.algorithm;

import edu.princeton.cs.algs4.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.DoubleStream;

public class BinarySearch {
    public static int rank(int key, int[] a){ // 数组必须是有序的
        int lo = 0;
        int hi = a.length - 1;
        while (lo <= hi){ // 被查找的键要么不存在，要么必然存在于 a[lo..hi] 之中
            int mid = lo + (hi - lo) / 2;
            if (key < a[mid]) hi = mid - 1;
            else if (key > a[mid]) lo = mid + 1;
            else return mid;
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] arrays = new int[]{1,2,3,4,5,6,7,8,9};
        int N = arrays.length;
        for (int i = 5; i <= N; i++) {
            int rank = rank(i, arrays);
            System.out.println(rank);
        }
        /*int a=0;
        int b=6;
        int c=0;
        int t=0;

        if (a > b) { t = a; a = b; b = t; }
        if (a > c) { t = a; a = c; c = t; }
        if (b > c) { t = b; b = c; c = t; }

        System.out.println(a +"-----" + b + "-----" + c);*/

        System.out.println("========================");
        int SIDES = 6;
        double[] dist = new double[2*SIDES+1];
        for (int i = 1; i <= SIDES; i++)
            for (int j = 1; j <= SIDES; j++)
                dist[i+j] += 1.0;
        for (int k = 2; k <= 2*SIDES; k++)
            dist[k] /= 36.0;
        DoubleStream stream = Arrays.stream(dist);

        int Number = 50;
        double[] a = new double[Number];
        for (int i = 0; i < Number; i++) {
            a[i] = StdRandom.random();
        }
//        Arrays.sort(a);
        for (int i = 0; i < Number; i++) {
            double x = 1.0*i/Number;
            double y = a[i]/2.0;
            double rw = 0.5/Number;
            double rh = a[i]/2.0;
            StdDraw.filledRectangle(x,y,rw,rh);
        }
    }
}
